﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using weixin.Models;

namespace weixin.Controllers
{
    /// <summary>
    /// domainController 的摘要说明
    /// </summary>
    public class domainController : IHttpHandler
    {
        private readonly Services.DomainService service = new Services.DomainService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            HttpCookie cookie = context.Request.Cookies.Get("token");
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { sucess = false, info = "请先登录", tologin = true }));
                return;
            }

            if (context.Request["Action"] == "query")
            {
                List<tb_Domains> list = service.Query();
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { sucess = true, info = "", data = list }));
                return;
            }

            if (context.Request["Action"] == "get")
            {
                tb_Domains model = service.GetById(int.Parse(context.Request["id"]));
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { sucess = model != null ? true : false, info = "", data = model }));
                return;
            }

            if (context.Request["Action"] == "del")
            {
                bool ret = service.Delete(context.Request["del_id"]);
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { sucess = ret, info = "" }));
                return;
            }

            if (context.Request["Action"] == "save")
            {
                int id = 0;
                int.TryParse(context.Request["id"], out id);
                tb_Domains model = service.GetById(id) ?? new tb_Domains() { adddate = DateTime.Now, id = 0 };
                model.name = context.Request["name"];
                model.appid = context.Request["appid"];
                model.host = context.Request["host"];
                model.state = context.Request["state"];
                model.scope = int.Parse(context.Request["scope"]);
                model.callback = !string.IsNullOrWhiteSpace(context.Request["callback"]) ? context.Request["callback"].Trim() : "http://" + context.Request["host"].Trim();
                model.jkey = !string.IsNullOrWhiteSpace(context.Request["jkey"]) ? context.Request["jkey"].Trim() : "data";
                if (string.IsNullOrEmpty(model.state))
                {
                    model.state = GenerateRandomNumber(10);
                }
                bool ret = service.AddOrUpdate(model);
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { sucess = ret, info = "" }));
                return;
            }
        }

        public string GenerateRandomNumber(int Length)
        {
            char[] constant =
             {
                'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
              };
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(constant.Length);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(constant.Length)]);
            }
            return newRandom.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}